package com.sfx.算法专题.双指针.n数之和;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-07-26
 * Time: 10:03
 */
public class Code_4_fourSumCount {
    static class Solution {
        public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
            int res = 0;
            //先遍历前两个四元组,然后将两数之和先保存起来
            Map<Integer,Integer> map = new HashMap<>();
            for(int i =0;i<nums1.length;++i) {
                for(int j =0;j<nums2.length;++j) {
                    map.put(nums1[i]+nums2[j],map.getOrDefault(nums1[i]+nums2[j],0) + 1);
                }
            }
            for(int i = 0;i<nums3.length;++i) {
                for(int j =0;j<nums4.length;++j) {
                    int sum = nums3[i] + nums4[j];
                    if(map.containsKey(-sum)) {
                        res += map.get(-sum);
                    }
                }
            }
            return res;
        }
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {-1,-1};
        int[] nums2 = {-1,1};
        int[] nums3 = {-1,1};
        int[] nums4 = {1,-1};
        solution.fourSumCount(nums1,nums2,nums3,nums4);
    }
}